package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SmIncome;
import com.ruoyi.project.system.domain.vo.Ranking;
import com.ruoyi.project.system.domain.vo.UserRecentlyIncome;
import com.ruoyi.project.system.mapper.SmIncomeMapper;
import com.ruoyi.project.system.service.ISmIncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 收入Controller
 */
@RestController
@RequestMapping("/system/income")
public class SmIncomeController extends BaseController {

    @Autowired
    private ISmIncomeService smIncomeService;

    @Autowired
    private SmIncomeMapper smIncomeMapper;

    @GetMapping("/recently")
    public AjaxResult userRecentlyIncome(@NotNull Integer pageNum) {
        Long userId = SecurityUtils.getUserId();
        List<UserRecentlyIncome> userRecentlyIncomes = smIncomeMapper.userRecentlyIncome(userId, pageNum * 10);
        return success(userRecentlyIncomes);
    }

    @GetMapping("/ranking")
    public AjaxResult incomeRanking() {
        List<Ranking> ranking = smIncomeMapper.ranking();
        for (Ranking r : ranking) {
            r.setUserName("***" + r.getUserName().substring(r.getUserName().length() / 2));
            r.setNickName("***" + r.getNickName().substring(r.getNickName().length() / 2));
        }
        return success(ranking);
    }

    @GetMapping("/cashOutAndRefund")
    public AjaxResult cashOutAndRefund() {
        Long userId = SecurityUtils.getUserId();
        return smIncomeService.cashOutAndRefund(userId);
    }

    @GetMapping("/cashOut")
    public AjaxResult cashOut() {
        Long userId = SecurityUtils.getUserId();
        return smIncomeService.carryOnCashOut(userId);
    }

    @GetMapping("/statistics")
    public AjaxResult statistics() {
        AjaxResult result = new AjaxResult();
        Long userId = SecurityUtils.getUserId();
        result.put("totalIncome", smIncomeMapper.totalIncome(userId));
        result.put("cashOut", smIncomeMapper.cashOut(userId));
        result.put("totalRefund", smIncomeMapper.totalRefund(userId));
        return result;
    }

    @PreAuthorize("@ss.hasPermi('system:income:query')")
    @GetMapping("/team")
    public AjaxResult teamList() {
        Long userId = SecurityUtils.getUserId();
        return success(smIncomeMapper.teamSmIncomeList(userId));
    }

    @PreAuthorize("@ss.hasPermi('system:income:list')")
    @GetMapping("/listByUserId")
    public AjaxResult listByUserId(@NotBlank Long userId) {
        return success(smIncomeMapper.selectSmIncomeListByUserId(userId));
    }

    /**
     * 查询收入列表
     */
    @PreAuthorize("@ss.hasPermi('system:income:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmIncome smIncome) {
        startPage();
        List<SmIncome> list = smIncomeMapper.selectSmIncomeList(smIncome);
        return getDataTable(list);
    }

    /**
     * 导出收入列表
     */
    @PreAuthorize("@ss.hasPermi('system:income:export')")
    @Log(title = "收入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmIncome smIncome) {
        List<SmIncome> list = smIncomeService.selectSmIncomeList(smIncome);
        ExcelUtil<SmIncome> util = new ExcelUtil<SmIncome>(SmIncome.class);
        util.exportExcel(response, list, "收入数据");
    }

    /**
     * 获取收入详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:income:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(smIncomeService.selectSmIncomeById(id));
    }

    /**
     * 新增收入
     */
    @PreAuthorize("@ss.hasPermi('system:income:add')")
    @Log(title = "收入", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmIncome smIncome) {
        return toAjax(smIncomeService.insertSmIncome(smIncome));
    }

    /**
     * 修改收入
     */
    @PreAuthorize("@ss.hasPermi('system:income:edit')")
    @Log(title = "收入", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmIncome smIncome) {
        return toAjax(smIncomeService.updateSmIncome(smIncome));
    }

}
